package servlets.customer;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.Cart;
import services.ServiceCart;

public class ServletViewCart extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();

        ServiceCart serviceCart = new ServiceCart();

        HttpSession session = request.getSession();
        String userIdsession = (String) session.getAttribute("userId");
        int userId = Integer.parseInt(userIdsession);
        List<Cart> carts = serviceCart.findCartByUserId(userId);

        String html = "<center>\n";
        if (carts.isEmpty()) {

            html += "<h4>Your cart is currently empty</h4>";

        } else {

            html += "<input type=\"button\" value =\"Remove All\" onclick=\"removeAllProducts()\">\n"
                    + "<table border=\"1\" id=\"cartTable\">\n"
                    + "<tr><th>Name</th> <th>Price</th> <th>Quantity</th> <th>Subtotal</th> <th>Remove product</th> </tr>";

            for (Cart cart : carts) {

                String name = cart.getProduct().getName();
                int price = cart.getProduct().getPrice();
                int quantity = cart.getAmount();
                int subtotal = price * quantity;
                int productId = cart.getProduct().getId();

                String remove = "<a onClick=removeProduct(" + productId + ")> Remove </a >";

                html += "<tr><td>" + name + "</td> <td>" + price + "</td> <td>" + quantity + "</td> <td>" + subtotal + "</td> <td>" + remove + " </tr>";
            }

            html += "</table>\n"
                    + "<input type=\"button\" value =\"Checkout\" onclick=\"buyProducts()\">\n";
        }

        html += "</center>";

        out.print(html);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ServiceCart serviceCart = new ServiceCart();

        int userId = Integer.parseInt(request.getParameter("androidUserId"));

        List<Cart> carts = serviceCart.findCartByUserId(userId);

        List<AndroidProduct> msgs = new ArrayList<AndroidProduct>();
        //AndroidProduct msg1 = new AndroidProduct(new URI("http://http://192.168.43.184:8084/ServletProject/best-price.png"), "Shoes", 150, 2);
//            AndroidProduct msg1 = new AndroidProduct(null, "Shoes", 150, 2);
//            msgs.add(msg1);
        for (Cart cart : carts) {
            String name = cart.getProduct().getName();
            int price = cart.getProduct().getPrice();
            int quantity = cart.getAmount();
            int pId = cart.getProduct().getId();

            AndroidProduct msg = new AndroidProduct(null, name, price, quantity);
            msg.setId(pId);
            msgs.add(msg);
        }

        Gson gson = new GsonBuilder().create();
        String jsonRepresentation = gson.toJson(msgs);

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        out.print(jsonRepresentation);
        //out.print("helloooooooo   "+request.getParameter("Var"));
        System.out.println(jsonRepresentation);
        //out.print(jsonRepresentation);
        System.out.println(jsonRepresentation);

    }
}
